var mditor;
var tale = new $.tale();
var attach_url = $('#attach_url').val();
// 每10分钟自动保存一次草稿
// var refreshIntervalId = setInterval("autoSave()", 10 * 60 * 1000);
Dropzone.autoDiscover = false;

$(document).ready(function() {

	mditor = window.mditor = Mditor.fromTextarea(document.getElementById('md-editor'));

	// Tags Input
	$('#tags').tagsInput({
		width : '100%',
		height : '35px',
		defaultText : '请输入文章标签'
	});

	$('.toggle').toggles({
		on : true,
		text : {
			on : '开启',
			off : '关闭'
		}
	});

	$("#multiple-sel").select2({
		width : '100%'
	});

	$('div.allow-false').toggles({
		off : true,
		text : {
			on : '开启',
			off : '关闭'
		}
	});

	if ($('#thumb-toggle').attr('thumb_url') != '') {
		$('#thumb-toggle').toggles({
			off : true,
			text : {
				on : '开启',
				off : '关闭'
			}
		});
		$('#thumb-toggle').attr('on', 'true');
		$('#dropzone').css('background-image', 'url(' + $('#thumb-container').attr('thumb_url') + ')');
		$('#dropzone').css('background-size', 'cover');
		$('#dropzone-container').show();
	} else {
		$('#thumb-toggle').toggles({
			off : true,
			text : {
				on : '开启',
				off : '关闭'
			}
		});
		$('#thumb-toggle').attr('on', 'false');
		$('#dropzone-container').hide();
	}
});

function istop(obj) {
	var this_ = $(obj);
	var on = this_.find('.toggle-on.active').length;
	var off = this_.find('.toggle-off.active').length;
	console.log(on + "dw" + off);
	if (on == 0) {
		$('#istop').val(1);
	}
	if (off == 0) {
		$('#istop').val(0);
	}
}
/**
 * 保存文章
 * @param status
 */
function subArticle(status) {
	var title = $('#articleForm input[name=title]').val();
	var content = mditor.value;
	var value = $("#multiple-sel").val(); //获取选中记录的value值  
	console.log(value + "___1");
	var text = $("#multiple-sel option:selected").text(); //获取选中记录的text值
	console.log(text + "_____2");
	if (title == '') {
		tale.alertWarn('请输入文章标题');
		return;
	}
	
	if (content == '') {
		tale.alertWarn('请输入文章内容');
		return;
	}
	if (value == '' || value == null) {
		tale.alertWarn('请选择标签');
		return;
	}
	$('#content-editor').val(content);
	$("#articleForm #status").val(status);
	$("#articleForm #categories").val($('#multiple-sel').val());
	var params = $("#articleForm").serialize();
	tale.post({
		url : '/blog/admin/article/modify/save',
		data : params,
		success : function(result) {
			if (result.msg == "success") {
				tale.alertOk({
					text : '文章保存成功',
					then : function() {
						setTimeout(function() {
							window.location.href = '/blog/admin/article';
						}, 500);
					}
				});
			} else {
				tale.alertError(result.msg || '保存文章失败');
			}
		}
	});
}


function allow_comment(obj) {
	var this_ = $(obj);
	var on = this_.find('.toggle-on.active').length;
	var off = this_.find('.toggle-off.active').length;
	if (on == 1) {
		$('#allow_comment').val(false);
	}
	if (off == 1) {
		$('#allow_comment').val(true);
	}
}